package com.hmall.api.config;

import com.hmall.api.fallback.ItemClientFallback;
import com.hmall.api.fallback.PayClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    /**
     * OpenFeign控制台日志
     * @return
     */
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    /**
     * OpenFeign接口的拦截器
     * @return
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                // 保存用户id给调用的接口使用
                Long user = UserContext.getUser();
                if(user!=null){
                    requestTemplate.header("user-info",user.toString());
                }
            }
        };
    }

    /**
     * 接口过载，防止报错，默认给个降级处理的逻辑，这里是注入该逻辑的实体类
     * @return
     */
    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }

    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }
}
